package cn.wolfcode.web.controller;


import cn.wolfcode.transfer.domain.Exam;
import cn.wolfcode.transfer.domain.ExamQuestion;
import cn.wolfcode.transfer.domain.InsertExam;
import cn.wolfcode.transfer.domain.Question;
import cn.wolfcode.transfer.qo.JsonResult;
import cn.wolfcode.transfer.service.IExamService;
import cn.wolfcode.transfer.qo.QueryObject;
import cn.wolfcode.transfer.util.QuestionUtil;
import cn.wolfcode.transfer.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;


    @RequiredPermission(name="试卷页面",expression="exam:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Exam> pageInfo = examService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "/exam/input";
    }


    @RequiredPermission(name="试卷删除",expression="exam:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           examService.delete(id);
        }
        return "/exam/preview";
    }


    @RequiredPermission(name="试卷新增/编辑",expression="exam:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Exam saveOrUpdate(InsertExam insertExam) {

        Exam exam = examService.insertExam(insertExam);

        return exam ;
    }


    @RequiredPermission(name="试卷新增/编辑",expression="exam:saveOrUpdate")
    @RequestMapping("/input")
    public String input() {
        return "/exam/input";
    }

    @RequiredPermission(name="试卷新增/编辑",expression="exam:saveOrUpdate")
    @RequestMapping("/preview")
    public String preview(Model model , Long id) {
        Exam exam = examService.get(id);
        List<ExamQuestion> examQuestions = exam.getExamQuestions();
        List<ExamQuestion> s = new ArrayList<>();
        List<ExamQuestion> M = new ArrayList<>();
        List<ExamQuestion> J = new ArrayList<>();
        for (ExamQuestion examQuestion : examQuestions) {
            Question question = examQuestion.getQuestion();
            if (question.getType()== QuestionUtil.单选.getValue()){
                s.add(examQuestion);
            }else if (question.getType()==QuestionUtil.多选.getValue()){
                M.add(examQuestion);
            }else {
                J.add(examQuestion);
            }

        }
        model.addAttribute("exam",exam);
        model.addAttribute("s",s);
        model.addAttribute("m",M);
        model.addAttribute("j",J);
        return "/exam/preview";
    }
}
